package com.ruoyi.baseData.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.baseData.domain.EquipmentConfig;
import com.ruoyi.baseData.domain.EquipmentLevel;
import com.ruoyi.baseData.domain.EquipmentTree;
import com.ruoyi.baseData.domain.dto.EquipmentTreeDto;
import com.ruoyi.baseData.domain.vo.EquipmentTreeVo;
import com.ruoyi.baseData.mapper.EquipmentConfigMapper;
import com.ruoyi.baseData.mapper.EquipmentTreeMapper;
import com.ruoyi.baseData.service.EquipmentTreeService;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.sql.QueryHelp;
import com.ruoyi.common.security.utils.SecurityUtils;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 *
 */
@Service
public class EquipmentTreeServiceImpl implements EquipmentTreeService {

    @Resource
    EquipmentTreeMapper equipmentTreeMapper;


    @Resource
    EquipmentConfigMapper equipmentConfigMapper;


    @Override
    public Page<EquipmentTreeVo> getEquipmentTreeByPage(EquipmentTreeDto pageEntity) {
        MPJLambdaWrapper<EquipmentTree> wrapper = new MPJLambdaWrapper<EquipmentTree>()
                .selectAll(EquipmentTree.class)//查询user表全部字段
                //记的测试阶段
                .eq(EquipmentConfig::getDelFlag, 0)
                .orderByAsc(EquipmentConfig::getCreateTime);
        wrapper = QueryHelp.getPredicateEx(wrapper, EquipmentTree.class, pageEntity);
        Page<EquipmentTreeVo> userList = equipmentTreeMapper.selectJoinPage(new Page<>(pageEntity.getCurrentPage(), pageEntity.getPageSize()),   EquipmentTreeVo.class, wrapper);
        return userList;
    }

    @Override
    public void addEquipmentTree(EquipmentTreeDto testDto) {
        String userName = SecurityUtils.getUsername();
//        MPJLambdaWrapper<EquipmentTree> wrapper = new MPJLambdaWrapper<EquipmentTree>()
//                .eq(EquipmentTree::getCode, testDto.getCode()).ne(EquipmentTree::getDelFlag, 1);

        EquipmentTree equipmentTree = new EquipmentTree();

        EquipmentConfig equipmentConfig  = equipmentConfigMapper.selectById(testDto.getLevelId());


        BeanUtils.copyProperties(testDto, equipmentTree, new String[]{"id"});

        equipmentTree.setLevel(equipmentConfig.getLevel());
        equipmentTree.setName(equipmentConfig.getName());
        equipmentTree.setCode(equipmentConfig.getCode());
        if(null != testDto.getParentId()) {
            equipmentTree.setParentId(testDto.getParentId());
        }
        equipmentTree.setDelFlag(false);
        equipmentTree.setCreateBy(userName);
        equipmentTree.setCreateTime(new Date());

        equipmentTreeMapper.insert(equipmentTree);

    }

    @Override
    public void updateEquipmentTree(EquipmentTreeDto testDto) {

    }

    @Override
    public void delEquipmentTree(EquipmentTreeDto testDto) {
        String userName = SecurityUtils.getUsername();
        EquipmentTree equipmentTree = equipmentTreeMapper.selectById(testDto.getId());
        if(null != equipmentTree) {
            equipmentTree.setDelFlag(true);
            equipmentTree.setUpdateBy(userName);
            equipmentTree.setUpdateTime(new Date());
            equipmentTreeMapper.updateById(equipmentTree);
        }
    }
}




